package codeTop;

import java.util.PriorityQueue;

public class q8 {
    public int myAtoi(String s) {
        if (s.isEmpty()) return 0;
        int idx = 0;
        while (idx < s.length()&&s.charAt(idx) == ' '){
            idx++;
        }
        boolean symbol = true;
        if (idx < s.length()&&s.charAt(idx) == '+') {
            symbol = true;
            idx++;
        } else if (idx < s.length()&&s.charAt(idx) == '-') {
            symbol = false;
            idx++;
        }
        while (idx < s.length()&&s.charAt(idx) == '0') {
            idx++;
        }
        long res = 0;
        while (idx < s.length()) {
            if (s.charAt(idx)>='0'&&s.charAt(idx)<='9') {
                res = res*10+(s.charAt(idx)-'0');
                if (res > Integer.MAX_VALUE) {
                    return symbol?Integer.MAX_VALUE:Integer.MIN_VALUE;
                }
            }else {
                return (int)res*(symbol?1:-1);
            }
            idx++;
        }
        return (int)res*(symbol?1:-1);
    }

    public static void main(String[] args) {
        q8 q = new q8();
        q.myAtoi("-91283472332");
    }
}
